#-*- coding:utf-8 -*-
from MyThread import MyThread
from Execute import *
from Read_Iplist import read_iplist
#读取iplist文档，并根据step值调用相应类，每台服务器生成一个线程执行
class Step_route():
    def __init__(self,ipListPath,step,local_nmon_path=None,local_result_path=None,analysis_path=None,root=None):
        self.ipListPath=ipListPath
        self.step=step
        self.local_nmon_path=local_nmon_path
        self.local_result_path=local_result_path
        self.analysis_path=analysis_path
        self.root=root
        if self.step=='parseResult':
            self.root.res.insert(0.0, '执行中。。。\n')
            parseResult(self.local_result_path,self.analysis_path,self.root)
        else:
            self._execute()
    def _execute(self):
        #读取iplist
        try:
            servers=read_iplist(self.ipListPath,self.root)
        except Exception as e:
            self.root.set_res('打开iplist.xlsx失败，请检查路径\n')
            return 0
        self.root.res.insert(0.0, '执行中。。。\n')
        threads=[]
        #启动线程
        a='self.step,servers.ip[i],servers.port[i],servers.uname[i],servers.passwd[i],servers.nmonpath[i],servers.command[i],self.local_nmon_path,self.local_result_path,self.analysis_path,self.root'
        cmd='MyThread(Execute,(%s))' % (a)
        for i in range(servers.rows):
            t=eval(cmd)
            threads.append(t)
        for i in range(servers.rows):
            threads[i].start()
        for i in range(servers.rows):
            threads[i].join()

